﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;

namespace SupplyChainManagement
{
    public class Observer
    {
        private delegate void UpdateEventHandler(string id);
        private event UpdateEventHandler DataChangedEvent;
        InventoryDetail form;

        public Observer(string id, InventoryDetail form)
        {
            this.DataChangedEvent += new UpdateEventHandler(Observer_DataChangedEvent);
            this.form = form;
        }

        private void OnChange(string id)
        {
            if (DataChangedEvent != null)
                DataChangedEvent(id);
        }

        public void Update(string id)
        {
            OnChange(id);
        }

        /// <summary>
        /// the event raised when update is called
        /// </summary>
        /// <param name="id"></param>
        private void Observer_DataChangedEvent(string id)
        {
            //do the database checking
            OdbcConnection dbMySQL = new OdbcConnection(MainForm.strConnect);
            try
            {
                int inventoryAmt = 0;
                //open database
                dbMySQL.Open();

                //to figure out the amount of this raw item
                OdbcCommand amountCmd = dbMySQL.CreateCommand();
                amountCmd.CommandText = "select * from rawitem where inv_id='" +
                    id + "'";
                OdbcDataReader amtReader = amountCmd.ExecuteReader();
                    
                while (amtReader.Read())
                {
                    inventoryAmt++;
                }
                amtReader.Close();
                amountCmd.Dispose();

                //get the minimum amount of item
                OdbcCommand minCommand = dbMySQL.CreateCommand();
                minCommand.CommandText = "select * from inventory where inv_id='" + id + "'";
                OdbcDataReader minReader = minCommand.ExecuteReader();
                int min = 0;
                string inv_name = null;
                while (minReader.Read())
                {
                    inv_name = minReader.GetString(1);
                    min = int.Parse(minReader.GetString(3));
                }

                //if inventory amount is the bare minimum or falls under the minimum
                if (inventoryAmt <= min)
                {
                    //forecasting --> ask user if they want to order more items
                    if (MessageBox.Show("Inventory is low! Do you wish to restock?", "Order confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        //New order form
                        OrderForecastForm form2 = new OrderForecastForm(id, inv_name, form);
                        form2.Show();
                    }
                }
                
                //close db connection
                dbMySQL.Close();
            }
            catch (OdbcException ex)
            {
                Console.WriteLine("Database error\n\n{0}", ex.ToString());
            }
            finally
            {
                if (dbMySQL != null) dbMySQL.Close();
            }
        }
    }
}
